package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.InStock;
import light.head.model.InStockItem;

import org.springframework.stereotype.Service;
/***
 * 物品入库信息
 * @author yy
 *
 */
@Service
public class InStockDAO extends BaseDAO<InStock, Integer>{
	private final static String sql_getByMc = " FROM InStock ins WHERE ins.mc = ? AND ins.type = ?";
	private final static String sql_findInStockItem = " FROM InStockItem isi WHERE isi.sc = ?";
	/**
	 * 查询本机构及所有子机构的入库计划
	 * @param ocs	机构编号数组
	 * @param pb	分页信息
	 * @param map	查询条件
	 */
	public RESULT findByMap(List<Integer> ocs, PageBean pb, Map<String,String> map) {
		if (null == ocs || ocs.size() == 0) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer(" FROM InStock ins WHERE 1=1");
		List<Object> list = new ArrayList<Object>();
		
		hql.append(" AND ins.oc IN (");
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String sort = map.get(Parm.SORT);
			String cn = map.get(Parm.CN);
			String name = map.get(Parm.NAME);
			String cu = map.get(Parm.CU);
			String cuName = map.get(Parm.CUN);
			
			if (isNotNull(cuName)) {
				list.add(cuName);
				hql.append(" AND ins.cu IN (SELECT u.code FROM UserInfo u WHERE u.name LIKE ?)");
			}
			if (isNotNull(cu)) {
				list.add(Integer.parseInt(cu));
				hql.append(" AND ins.cu = ?");
			}
			if (isNotNull(name)) {
				list.add("%" + name + "%");
				hql.append(" AND ins.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if (isNotNull(sort)) {
				list.add(Byte.parseByte(sort));
				hql.append(" AND ins.sort = ?");
			}
			if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND ins.cu in (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
			}
		}
		hql.append(" ORDER BY ins.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	/**
	 * 根据oc 获取入库信息
	 * @param oc	机构编号
	 * @param pb	分页信息
	 * @param map	查询条件
	 */
	public RESULT findByMap(Integer oc, PageBean pb, Map<String,String> map) {
		if (null == oc) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer(" FROM InStock ins WHERE ins.deleted = 0");
		List<Object> list = new ArrayList<Object>();
		
		hql.append(" AND ins.oc = ?");
		list.add(oc);
		
		if (map != null) {
			String cn = map.get(Parm.CN);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String gn = map.get(Parm.GN);
			
			if (isNotNull(gn)) {
				list.add("%" + gn + "%");
				hql.append(" AND ins.code in (SELECT g.gc FROM InStockItem g WHERE g.gn like ?)");
			}
			if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND ins.cu in (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
			}
			if (isNotNull(time1)) {
				hql.append(" AND ins.ct >= ?");
				list.add(DateUtils.getDateByStr(time1, null));
			}if (isNotNull(time2)) {
				hql.append(" AND ins.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, null));
			}
		}
		hql.append(" ORDER BY ins.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 通过MC 和 type  获取指定的入库信息
	 * @param mc
	 * @return
	 */
	public InStock getByMc(Integer mc, Byte type) {
		List<InStock> list = find(sql_getByMc, mc, type);
		return list.size() > 0 ? list.get(0) : null;
		
	}
	
	public List<InStockItem> findInStockItem(Integer code) {
		if (null == code)
			return null;
		List<InStockItem> list = find(sql_findInStockItem, code);
		return list;
	}
}
